<?php


namespace app\user\controller;

use app\user\logic\ShareLinkLogic;
use EasyWeChat\Factory;
use app\common\model\MiniApp as MiniAppModel;
use think\Controller;
use app\common\model\WechatShareLink as WechatShareLinkModel;
use think\Exception;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Env;
use think\facade\Request;
use EasyWeChat\Kernel\Messages\Article;


class Url extends Controller
{
    public function index()
    {

    }

    //生成扩展链接
    public function addUrl()
    {
        $img = Request::param('img');
        $url = Request::param('url');
        $title = Request::param('title',"京东带货");
        $url_mode = Request::param('url_mode',3); //1文件，2腾讯视频地址
        $content = Request::param('content', '点击查看视频内容');

        $config = ShareLinkLogic::getAccount();
        $config['response_type'] = "object";
        $app = Factory::officialAccount($config);
        $no=rand(20000000,99999999). time();
        //上传素材
        //  $result = $app->material->uploadImage($img);

        //   $this->result($result, 400, '请求成功');

        $mini_app = MiniAppModel::where('code', 'url')->find();
        $path = "pages/movie/index?promote_user_id=1&mini_app_code=movie&no=".$no;

        // 上传单篇图文
        $article = new Article([
            'title' => '点击分享链接',
            'thumb_media_id' => ShareLinkLogic::getCoverMediaId(),
            'show_cover' => 0, // 是否在文章内容显示封面图片
            "content_source_url" => ShareLinkLogic::getContentSourceUrl(),
            'content' => self::getArticleContent($mini_app, $path),
            //...
        ]);
        try{
            $res3 = $app->material->uploadArticle($article);
            $res= $app->broadcasting->sendNews($res3->media_id,ShareLinkLogic::getPushOpenids());
            //超出公众号最大限制
        }catch (Exception $e){
            //清除缓存
            Cache::rm('cover_media_id');
            Cache::rm('test_cover_url');
            Cache::rm('openids');
            Cache::rm('cover_url');
            Cache::rm('test_cover_url');
        }

        try{
            $msg_id=$res->msg_id ;
        }catch (Exception $e){
            $this->result($e->getMessage(),400,'生成失败');
        }
        $data = [
            'user_id'=>4,
            'app_id'=>$config['app_id'],
            'created_id'=>4,
            'no'=>$no,
            'mini_app_code'=>'url',
            "product_id"=>0,
            'title' => $title,
            'msg_id'=> $msg_id,
            "url" => '',
            "url_mode" => $url_mode,
            "video_url" => '',
            'add_time'=>time()
        ];
        //存数据库
        $res2 = WechatShareLinkModel::create( $data);

        //轮询60秒，60秒还没生成成功，直接删掉，重新生成
        for ($i = 0; $i < 41; $i++) {
            sleep(1);
            $link = WechatShareLinkModel::where('msg_id', $msg_id)->find();
            if ($link && $link->url != '') {
                //return $link;
                $this->result($link, 200, '生成成功');
            }
        }
        $this->result('',400,'链接生成超时');

    }

    //生成一个视频文章的content部分
    public function getArticleContent($mini_app, $path, $user_id=0)
    {
        $mini_app_code = $mini_app->code;
        $cover = ShareLinkLogic::getVideoArticleBtnCover();
        $platform_appid = Config::get('miniprogram.wx.appid');


        $content = "<p style=\"text-align: left;\">
    <span style=\"text-indent: 2em;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;text-align: justify;color: #ea445a;font-size: 17px;\" data-darkmode-color-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-color-16027602271042=\"rgb(234, 68, 90)\"><strong data-darkmode-color-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-color-16027602271042=\"rgb(234, 68, 90)\">&#8203;¥</strong></span>
    <span style=\"text-indent: 2em;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;text-align: justify;color: #ea445a;font-size: 24px;margin-left: -2px;\" data-darkmode-color-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-color-16027602271042=\"rgb(234, 68, 90)\"><strong data-darkmode-color-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-color-16027602271042=\"rgb(234, 68, 90)\">19.90</strong></span>
    <span style=\"line-height: 38px;right:0px;float: right;font-size: 14px;color: rgb(165, 165, 165);\" data-darkmode-color-16027602271042=\"rgb(163, 163, 163)\" data-darkmode-original-color-16027602271042=\"rgb(165, 165, 165)\" data-style=\"line-height: 38px; right: 0px; float: right; font-size: 14px; color: rgb(165, 165, 165);\" class=\"js_darkmode__0\">京东</span>
</p>
<p>
    <span style=\"font-size: 17px;\"><strong>【京东速运】沾化冬枣 脆甜冬枣时令新鲜水果2.5kg 非陕西大荔冬枣 京东生鲜现摘</strong></span>
</p>
<p style=\"display:none;\">
    <span style=\"left:0px;font-size: 14px;color: rgb(165, 165, 165);\" data-darkmode-color-16027602271042=\"rgb(163, 163, 163)\" data-darkmode-original-color-16027602271042=\"rgb(165, 165, 165)\" data-style=\"left: 0px; font-size: 14px; color: rgb(165, 165, 165);\" class=\"js_darkmode__2\">已售 0</span>
    <span style=\"right:0px;float: right;font-size: 14px;color: rgb(165, 165, 165);\" data-darkmode-color-16027602271042=\"rgb(163, 163, 163)\" data-darkmode-original-color-16027602271042=\"rgb(165, 165, 165)\" data-style=\"right: 0px; float: right; font-size: 14px; color: rgb(165, 165, 165);\" class=\"js_darkmode__3\">商品来自京东</span>
</p>
<p style=\"display:none;width:100%;margin-top: 10px;border-top:1px dashed #cccccc;height: 1px;overflow:hidden;\" data-style=\"display: none; width: 100%; margin-top: 10px; border-top: 1px dashed rgb(204, 204, 204); height: 1px; overflow: hidden;\" class=\"js_darkmode__4\">
    <br>
</p>
<section style=\"
    text-align: center;
    margin-top: 8px;
    background: #ea445a;
    height: 48px;
    border-radius: 2px;
    color: #FFFFFF;
    line-height: 48px;
    font-size: 17px;
    \" data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\">
    <strong data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\">点击这里购买此商品</strong>
    <svg style=\"padding-top: 9px;overflow: visible;pointer-events: none;max-width: none !important;isolation: isolate;height:24px;width:24px\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0\" y=\"0\" viewBox=\"0 0 18 18\" xml:space=\"preserve\" data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\">
        <g data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\">
            <g transform=\"translate(10 10)\" data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\">
                <g style=\"pointer-events:visible;\" data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\">
                    <circle cx=\"1\" opacity=\"0.7\" cy=\"1\" fill=\"#fff\" r=\"12.5\" data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\">
                        <animate attributeName=\"r\" values=\"10.5;13;10.5\" begin=\"0s\" dur=\"1.2s\" repeatCount=\"indefinite\" data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\">
                        </animate>
                    </circle>
                    <circle cx=\"1\" cy=\"1\" fill=\"#fff\" r=\"4.16\" data-darkmode-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-original-bgcolor-16027602271042=\"rgb(234, 68, 90)\" data-darkmode-color-16027602271042=\"rgb(255, 255, 255)\" data-darkmode-original-color-16027602271042=\"rgb(255, 255, 255)\"></circle>
                </g>
            </g>
        </g>
    </svg>
</section>
";
        return $content;
    }

    //获取视频文章封面封面
    public static function getVideoArticleBtnCover()
    {
        $cover_url = Cache::get('cover_url');

        if ($cover_url) {
            return $cover_url;
        } else {
            //获取数据库公众号，配置文件
            //$config = Config::get('miniprogram.officialAccount');
            $config = ShareLinkLogic::getAccount();
            $app = Factory::officialAccount($config);
            $result22 = $app->material->uploadImage(Env::get('config_path') . 'yingshi_btn.png');
            $media_id = $result22->media_id;
            $cover_url = $result22->url;
            Cache::set('cover_url', $cover_url, 10 * 60);
            return $cover_url;
        }
    }
}
